/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/

#include "os_obj.h"
#define _SYSTASK_MODULE_
#include "sys_task.h"
#undef _SYSTASK_MODULE_
#include "pincfg.h"
#include "sys_init.h"
#include "com_obj.h"
#include "common.h"
#include "bsp_hal.h"
#include "crc_ext.h"
#include "str_ext.h"

void sys_led_ctr(void);
void system_info(char pos);
void sys_ui_tst(void);
int core_main(int argc, char *argv[]);

char * listn[] = 
{
    [0] = {"new""new2"}, //编译器链接为一个字符串
    [1] = {"en"},
};

char upt[]="Jkcnt = kill and buf  ";

/*****************************************************************************
*
* @brief   system task.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_system(ULONG thread_input)
{
	(void)thread_input;

	uint32_t us_tick_s, us_tick_end;

	sys_init();

	system_info(0);
    
	str_to_upper_with_end(upt, '=' );
	
	while (1)
	{
        bsp_dwt_check();
        
		us_tick_s = app_tick_us_get();

		sys_st.u_tick = app_tick_get();

		if (sys_st.b_coremark)
		{
			sys_st.b_coremark = 0;
		}
		
		pulse_create(&sys_st.pulse, sys_st.u_tick);
		if (sys_st.pulse.bPulse_1s)
		{
			TimeRun(&sys_st.rtc_time);
			O_LED1.dir_out_pp();
			O_LED1.tog();
		}
        
		sys_st.sig_time = time_to_sig(&sys_st.rtc_time);
		time_sig_to_time(sys_st.sig_time, &sys_st.rtc_time);
		crc16_modbus_calc(sys_st.ui.buff, 16);
		crc16_modbus_calc(sys_st.ui.buff, 16);
        
        
        
		sys_led_ctr();

		us_tick_end = app_tick_us_get();

		sys_st.exe_time_us = us_tick_end - us_tick_s;

        if(sys_st.exe_time_us_max < sys_st.exe_time_us)
        {
            sys_st.exe_time_us_max = sys_st.exe_time_us;
        }
        
		NOP1();
		osDelay(ms_ticks(10));
		NOP1();
	}
}

/*****************************************************************************
*
* @brief   led control.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_led_ctr(void)
{
	if (sys_st.pulse.bPulse_1s)
	{
		//O_LED1.dir_out_pp();
		//O_LED1.tog();
	}

	sys_st.led_obj.IncCoef = 50;
	sys_st.led_obj.min = 1;
	sys_st.led_obj.max = 900;
	sys_st.led_obj.max_keep_time_sv = 2;
	sys_st.led_obj.min_keep_time_sv = 2;
	led_breath_handle(&sys_st.led_obj);

	app_tick_duty_set(4, sys_st.led_obj.Out);
}

/*****************************************************************************
*
* @brief   task switch hook.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_sw_hook(char pos)
{

}

/*****************************************************************************
*
* @brief   system info.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void system_info(char pos)
{
	RCC_ClockType RCC_Clocks;
	RCC_GetClocksFreq(&RCC_Clocks);

	dbg_puts("\n\n");
	dbg_puts("==========================================\n");
	dbg_print("compile time: %s  %s\n", __DATE__, __TIME__);
	dbg_puts("RTOS : ThreadX\n");
	dbg_print("AHB CLK: %dHz\n", RCC_Clocks.AHBCLK_Freq);
	dbg_print("APB1 CLK: %dHz\n", RCC_Clocks.APB1CLK_Freq);
	dbg_print("APB2 CLK: %dHz\n", RCC_Clocks.APB2CLK_Freq);
	dbg_print("ADC CLK: %dHz\n", RCC_Clocks.ADCCLK_Freq);
	dbg_puts("==========================================\n");
}

/*****************************************************************************
* @brief   task switch hook.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_pre_init(char pos)
{
	sys_st.ui.str_len = 12;
	sys_st.ui.ip_in = 0x72;
	sys_st.ui.min = INT32_MIN;
	sys_st.ui.max = INT32_MAX;

	sys_st.b_coremark = 0;
}
OBJ_INIT_APP_EXPORT(sys_pre_init);

/*****************************************************************************
* @brief   system ui test.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_ui_tst(void)
{
	if (sys_st.ui.ch_add)
	{
		ui_num_text_input_add(&sys_st.ui.obj, sys_st.ui.ch_add);
		sys_st.ui.ch_add = 0;
	}

	if (sys_st.ui.del_req)
	{
		ui_num_text_input_del(&sys_st.ui.obj);
		sys_st.ui.del_req = 0;
	}

	if (!sys_st.ui.rst_comp)
	{
		ui_text_input_reset(&sys_st.ui.obj, sys_st.ui.buff, sys_st.ui.str_len);
		ui_num_text_input_rang(&sys_st.ui.obj, 1, sys_st.ui.min, sys_st.ui.max);
		ui_num_text_input_inpn_set(&sys_st.ui.obj, sys_st.ui.ip_in >> 4, sys_st.ui.ip_in & 0xF);
		sys_st.ui.rst_comp = 1;
	}
}

uint32_t rt_tick_get(void)
{
	return sys_st.u_tick;
}
